package com.audaque.springboot.foshanupload.reactor.webfluxdemo.handler;

import com.audaque.springboot.foshanupload.reactor.webfluxdemo.model.qo.QueryRequest;
import com.audaque.springboot.foshanupload.reactor.webfluxdemo.service.iface.MusicDetailService;
import com.audaque.springboot.foshanupload.reactor.webfluxdemo.model.pojo.MusicDetail;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.util.concurrent.ExecutionException;

/**
 * @author zgb
 * @desc
 * @date 2023-07-24 21:21:26
 */

@Component
public class MusicDetailHandler {

    @Autowired
    private MusicDetailService musicDetailService;

    public Mono<ServerResponse> getMusic(ServerRequest request) {
        int page = NumberUtils.toInt(request.queryParam("page").orElse("1"));
        int size = NumberUtils.toInt(request.queryParam("size").orElse("20"), 1);
        Mono<QueryRequest> mono = request.bodyToMono(QueryRequest.class);
        Mono<QueryRequest> objectMono = mono.subscribeOn(Schedulers.boundedElastic())
                .publishOn(Schedulers.boundedElastic()); // <- this
        QueryRequest block = objectMono.block();
        Mono<Page<MusicDetail>> music = musicDetailService.getMusic(page, size, block);
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).bodyValue(music);
    }

}
